Page({

	onLoad: function (options) {
		wx.showLoading({
			title: 'loading...',
		})
		// console.log('app_jump_openid')
		if (!options.page_name) {
			var page_name = ''
		} else {
			var page_name = options.page_name
		}
		if (!options.from) {
			var from_page = ''
		} else {
			var from_page = options.from
		}
		this.get_wx_openid(page_name, from_page)
	},

	// 获取openid,usr_sign,usr_all_info
	get_wx_openid(page_name, from_page) {
		// 重新获取openId, sessionKey
		wx.login({
			success: res => {
				// 发送 res.code 到后台换取 openId, sessionKey, unionId
				if (res.code) {
					//获取openid，sessionKey
					wx.request({
						url: 'https://x.quhuichang.cn/api/code_get_openid/',
						data: {
							code: res.code,
							signstr: 'dream_ren_3016',
						},
						header: {
							'content-type': 'application/json'
						},
						success: function (res) {
							if (res.statusCode == 200) {
								// console.log(res.data)
								if (res.data.openid) {
									// 清除
									wx.removeStorageSync('usr_wx_openid');
									wx.removeStorageSync('usr_wx_phone');
									wx.removeStorageSync('usr_all_info');
									wx.removeStorageSync('usr_sign');
									// 存储
									wx.setStorageSync('usr_wx_openid', res.data.openid);
									wx.setStorageSync('usr_sign', res.data.usr_sign);
									wx.setStorageSync('usr_all_info', res.data.usr_all_info);
									if (res.data.usr_phone) {
										wx.setStorageSync('usr_wx_phone', res.data.usr_phone);
									}
									// 登录成功跳转网址
									if (page_name) {
										var gotourl = '/pages/' + page_name + '/' + page_name + '?from=' + from_page + '&goto=app_jump_openid'
									} else {
										var gotourl = '/pages/index/index?from=' + from_page + '&goto=app_jump_openid'
									}
									wx.reLaunch({
										url: gotourl
									})
									return false
								} else {
									wx.showModal({
										title: '获取信息失败',
										content: '是否重新获取信息',
										success(res) {
											if (res.confirm) {
												// console.log('用户点击确认')
												wx.reLaunch({
													url: '/pages/app_jump_openid/app_jump_openid?page_name=' + page_name + '&from=' + from_page + '&goto=app_jump_openid_reLaunch'
												})
												return false
											} else if (res.cancel) {
												wx.exitMiniProgram()
												// console.log('用户点击取消')
											}
										}
									})
									return false
								}
							} else {
								wx.showToast({
									title: '连接服务器失败',
									icon: "error",
									duration: 4000,
									mask: true,
								})
								return false
								// console.log(res.errMsg)
							}
						}
					})
				} else {
					wx.showToast({
						title: '登录失败！',
						icon: 'error',
						mask: false, //是否显示透明蒙层，防止触摸穿透，默认：false
						duration: 3000
					})
					// console.log('登录失败！' + res.errMsg)
				}
			}
		})
		// 重新获取openId, sessionKey 结束
	},


})